package com.paireach.easypermissions01;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, EasyPermissions.PermissionCallbacks {

    private static final int RC_CAMERA_PERM = 100;//camera权限
    private static final String TAG = "MainActivity";
    private Context context = this;
    private Button btn_camera;
    private Button btn_location;
    private Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);

        btn_camera = (Button) findViewById(R.id.btn_camera);
        btn_location = (Button) findViewById(R.id.btn_location);

        btn_camera.setOnClickListener(this);
        btn_location.setOnClickListener(this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "onActivityResult,requestCode=" + requestCode + ",resultCode=" + resultCode);
        /**
         * requestCode是requestPermissions(...)中设置的
         * resultCode不需要判断，因为不管你给不给权限，resultCode == RESULT_CANCELED ，也就是0.
         * 所以需要重新判断有没有权限
         */
        if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
            toast.setText("从设置界面回来");
            toast.show();
            getCamera();//
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_camera:
                getCamera();
                break;
            case R.id.btn_location:

                break;
        }
    }

    @AfterPermissionGranted(RC_CAMERA_PERM)
    private void getCamera() {
        String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION};
        if (EasyPermissions.hasPermissions(context, Manifest.permission.CAMERA)) {
            toast.setText("已经有Camera权限了");
            toast.show();
        } else {
            EasyPermissions.requestPermissions(MainActivity.this, "App需要拍照权限", RC_CAMERA_PERM, Manifest.permission.CAMERA);
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        toast.setText("同意，开始执行拍照");
        toast.show();
        Log.d(TAG, "onPermissionsGranted,requestCode=" + requestCode + ",perms=" + perms.toString());
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        /**
         *  默认requestCode=16061，即AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE
         *  默认的提示是英文的，所以需要自定义设置
         */

        new AppSettingsDialog.Builder(this).build().show();

        //自定义设置
//        new AppSettingsDialog.Builder(this)
//                .setTitle("拍照权限")
//                .setRationale("缺少拍照权限，将要跳转到设置界面")
//                .setPositiveButton("同意")
//                .setNegativeButton(" ")//"" 是无法隐藏取消按钮的，必须加空格
//                .setRequestCode(requestCode)
//                .build()
//                .show();
        Log.d(TAG, "onPermissionsDenied,requestCode=" + requestCode + ",perms=" + perms.toString());
    }
}
